import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class Testrole:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.role
    @pytest.mark.parametrize('roleinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/role/1_get_role_list.yaml'))
    def test_get_role_list(self, roleinfo):
        # 获取role列表接口-正常
        base_url = datas.baseUrl + Testrole.url_unify
        url = base_url + roleinfo['request']['url'].replace('{namespace}', 'default')
        method = roleinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == roleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.role
    @pytest.mark.parametrize('roleinfo', YamlUtil().read_testcase_yaml('v25.06/resourcemanage/role/2_create_role.yaml'))
    def test_create_role(self, roleinfo):
        # 创建role接口-正常
        base_url = datas.baseUrl + Testrole.url_unify
        url = base_url + roleinfo['request']['url'].replace('{namespace}', 'default')
        method = roleinfo['request']['method']
        data = roleinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        rolename = req.json()['metadata']['name']
        globals()['rolename'] = rolename
        assert req.status_code == roleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.role
    @pytest.mark.parametrize('roleinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/role/3_get_specific_role.yaml'))
    def test_get_specific_role(self, roleinfo):
        # 获取指定role接口-正常
        base_url = datas.baseUrl + Testrole.url_unify
        url = (base_url + roleinfo['request']['url']
               .replace('{namespace}', 'default').replace('{rolename}', globals()['rolename']))
        method = roleinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == roleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.role
    @pytest.mark.parametrize('roleinfo', YamlUtil().read_testcase_yaml('v25.06/resourcemanage/role/4_update_role.yaml'))
    def test_update_role(self, roleinfo):
        # 更新指定role接口-正常
        base_url = datas.baseUrl + Testrole.url_unify
        url = (base_url + roleinfo['request']['url']
               .replace('{namespace}', 'default').replace('{rolename}', globals()['rolename']))
        method = roleinfo['request']['method']
        roleinfo['request']['data'] = globals()['updatereqbody']
        data = roleinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == roleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.role
    @pytest.mark.parametrize('roleinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/role/5_update_section_role.yaml'))
    def test_update_section_role(self, roleinfo):
        # 部分更新指定role的接口-正常
        base_url = datas.baseUrl + Testrole.url_unify
        url = (base_url + roleinfo['request']['url']
               .replace('{namespace}', 'default').replace('{rolename}', globals()['rolename']))
        method = roleinfo['request']['method']
        data = roleinfo['request']['data']
        header = roleinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == roleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.role
    @pytest.mark.parametrize('roleinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/role/6_delete_specific_role.yaml'))
    def test_delete_specific_role(self, roleinfo):
        # 删除role接口-正常
        base_url = datas.baseUrl + Testrole.url_unify
        url = (base_url + roleinfo['request']['url']
               .replace('{namespace}', 'default').replace('{rolename}', globals()['rolename']))
        method = roleinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == roleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.role
    @pytest.mark.parametrize('roleinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/role/1_get_role_list_exception.yaml'))
    def test_get_role_list_exception(self, roleinfo):
        # 获取role列表接口-异常
        base_url = datas.baseUrl + Testrole.url_unify
        url = base_url + roleinfo['request']['url'].replace('{namespace}', 'default')
        method = roleinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == roleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.role
    @pytest.mark.parametrize('roleinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/role/2_create_role_exception.yaml'))
    def test_create_role_exception(self, roleinfo):
        # 创建role接口-异常
        base_url = datas.baseUrl + Testrole.url_unify
        url = base_url + roleinfo['request']['url'].replace('{namespace}', 'default')
        method = roleinfo['request']['method']
        data = roleinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == roleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.role
    @pytest.mark.parametrize('roleinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/role/3_get_specific_role_exception.yaml'))
    def test_get_specific_role_exception(self, roleinfo):
        # 获取指定role接口-异常
        base_url = datas.baseUrl + Testrole.url_unify
        url = (base_url + roleinfo['request']['url']
               .replace('{namespace}', 'default').replace('{rolename}', globals()['rolename']))
        method = roleinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == roleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.role
    @pytest.mark.parametrize('roleinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/role/4_update_role_exception.yaml'))
    def test_update_role_exception(self, roleinfo):
        # 更新指定role接口-异常
        base_url = datas.baseUrl + Testrole.url_unify
        url = (base_url + roleinfo['request']['url']
               .replace('{namespace}', 'default').replace('{rolename}', globals()['rolename']))
        method = roleinfo['request']['method']
        roleinfo['request']['data'] = globals()['updatereqbody']
        data = roleinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == roleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.role
    @pytest.mark.parametrize('roleinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/role/5_update_section_role_exception.yaml'))
    def test_update_section_role_exception(self, roleinfo):
        # 部分更新指定role的接口-异常
        base_url = datas.baseUrl + Testrole.url_unify
        url = (base_url + roleinfo['request']['url']
               .replace('{namespace}', 'default').replace('{rolename}', globals()['rolename']))
        method = roleinfo['request']['method']
        data = roleinfo['request']['data']
        header = roleinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == roleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.role
    @pytest.mark.parametrize('roleinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/role/6_delete_specific_role_exception.yaml'))
    def test_delete_specific_role_exception(self, roleinfo):
        # 删除role接口-异常
        base_url = datas.baseUrl + Testrole.url_unify
        url = (base_url + roleinfo['request']['url']
               .replace('{namespace}', 'default').replace('{rolename}', globals()['rolename']))
        method = roleinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == roleinfo['validate'][0]['equals']['status_code']
